import axios from 'axios'
const Q = require('q');
paiurl:'http://47.96.67.141:10045/';

let http = axios.create({
  baseURL:'http://47.96.67.141:10045/',
  withCredentials:true,
  headers:{
    'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
  },
  transformRequest: [function (data) {
    let newData = '';
    for (let k in data) {
      if (data.hasOwnProperty(k) === true) {
        newData += encodeURIComponent(k) + '=' + encodeURIComponent(data[k]) + '&';
      }
    }
    return newData;
  }]
});

function apiAxios(method, url, params,response) {
  http({
    method: method,
    url: 'http://47.96.67.141:10045/'+url,
    data: method === 'POST' || method === 'PUT' ? params : null,
    params: method === 'GET' || method === 'DELETE' ? params : null,
  }).then(function (res) {
    response(res);
  }).catch(function (err) {
    response(err);
  })
}

export default {



  get: function (url, params, response) {
    return apiAxios('GET', url, params, response)
  },
  post: function (url, params, response) {
    console.log('post----------------------------')
    return apiAxios('POST', url, params, response)
  },
  post2: function (url, params, response) {
    return apiAxios('POST', url, params, response)
  },
  put: function (url, params, response) {
    return apiAxios('PUT', url, params, response)
  },
  delete: function (url, params, response) {
    return apiAxios('DELETE', url, params, response)
  },

  apiWay:function(type,url,data){
    var deferred = Q.defer();
    axios({
      method:type,
      url: this.paiurl + url,
      headers:{'Authorization':'','Content-Type':'application/json;charset=utf-8'},
      data:data,
    }).then(function (response) {
      deferred.resolve(response);
    }).catch(function (error) {
      console.log(error)
    });
    return deferred.promise;
  }
}
